package com.coding.v1;


import java.sql.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @description <指定数据表,生成对应entity打印在控制台>
 * @author: <chancezhou>
 * @Date: 2023/7/26 11:14
 */
public class EntityGenerator {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTC";
        String username = "root";
        String password = "root123";
        String tableName = "t_user"; // 指定你想要生成Entity的表名

        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet columns = metaData.getColumns(null, null, tableName, null);

            Map<String, String> columnMappings = new HashMap<>();
            while (columns.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                String columnType = columns.getString("TYPE_NAME");
                String javaType = sqlTypeToJavaType(columnType);
                columnMappings.put(columnName, javaType);
            }

            generateEntityClass(tableName, columnMappings);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        }

    private static String sqlTypeToJavaType(String sqlType) {
        // 这里简单示例，你可以根据实际数据库类型进行映射
        switch (sqlType) {
            case "VARCHAR":
            case "CHAR":
                return "String";
            case "INT":
                return "int";
            case "BIGINT":
                return "long";
            case "DECIMAL":
                return "BigDecimal";
            case "DATE":
            case "DATETIME":
                return "String";
            // Add more mappings as needed for your database
            default:
                return "Object";
        }
    }

//    private static String sqlTypeToJavaType(String sqlType) {
//        // 这里简单示例，你可以根据实际数据库类型进行映射
//        return switch (sqlType) {
//            case "VARCHAR","CHAR" -> "String";
//            case "INT" -> "int";
//            case "BIGINT"-> "long";
//            case "DECIMAL"-> "BigDecimal";
//            case "DATE", "DATETIME"-> "LocalDateTime";
//            // Add more mappings as needed for your database
//            default ->{
//                yield "Object";
//            }
//
//        }
//    }

    private static void generateEntityClass(String tableName, Map<String, String> columnMappings) {
        StringBuilder classBuilder = new StringBuilder();
        classBuilder.append("public class ").append(toCamelCase(tableName)).append(" {\n");

        for (Map.Entry<String, String> entry : columnMappings.entrySet()) {
            String columnName = entry.getKey();
            String javaType = entry.getValue();
            String fieldName = toCamelCase(columnName);
            classBuilder.append("    private ").append(javaType).append(" ").append(fieldName).append(";\n");
        }

        classBuilder.append("}");

        System.out.println(classBuilder.toString());
    }

    private static String toCamelCase(String input) {
        StringBuilder result = new StringBuilder();
        String[] words = input.toLowerCase().split("_");
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            result.append(i == 0 ? word : capitalize(word));
        }
        return result.toString();
    }

    private static String capitalize(String input) {
        return input.substring(0, 1).toUpperCase() + input.substring(1);
    }
}
